package avicit.bdp.dcs.semistructure.rest;

import avicit.bdp.dcs.semistructure.dto.TemplateDTO;
import avicit.bdp.dcs.semistructure.service.TemplateService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/bdp/dcs/template/TemplateRest")
@Api(tags = "模板管理接口")
public class TemplateRest {

    private static final Logger logger = LoggerFactory.getLogger(TemplateRest.class);

    @Autowired
    private TemplateService templateService;

    @GetMapping("/getTemplateList")
    @ApiOperation(value = "查询模板列表")
    public ResponseMsg<QueryRespBean<TemplateDTO>> getTemplateList(@ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按名称升序：3，按名称倒序：4", name = "sort")  @RequestParam(required = false) Integer sort,
                                                                   @ApiParam(value = "模板名称", name = "templateName") @RequestParam(required = false) String templateName,
                                                                   @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
                                                                   @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
                                                                   @ApiParam(value = "是否过滤已经关联目录的模板", name = "isFilter", required = true) @RequestParam(defaultValue = "false") Boolean isFilter) {
        ResponseMsg<QueryRespBean<TemplateDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.getTemplateList(sort, templateName, pageNo, pageSize, isFilter));
        return responseMsg;
    }

    @PostMapping("/createTemplate")
    @ApiOperation(value = "新增模板")
    public ResponseMsg createTemplate(@ApiParam(value = "TemplateDTO", name = "TemplateDTO") @RequestBody @Valid TemplateDTO templateDto) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg<>();
        templateService.createTemplate(templateDto);
        return responseMsg;
    }

    @PostMapping("/updateTemplate")
    @ApiOperation(value = "修改模板")
    public ResponseMsg updateTemplate(@ApiParam(value = "TemplateDTO", name = "TemplateDTO") @RequestBody @Valid TemplateDTO templateDto) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg<>();
        templateService.updateTemplate(templateDto);
        return responseMsg;
    }

    @DeleteMapping("/deleteTemplateByIds")
    @ApiOperation(value = "删除模板")
    public ResponseMsg<Boolean> deleteTemplateByIds(@ApiParam(value = "主键id(多个逗号分隔)", name = "templateIds", required
            = true) @RequestBody TemplateDTO templateDTO) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        templateService.deleteTemplateByIds(templateDTO);
        return responseMsg;
    }

    @DeleteMapping("/deleteTempTemplate")
    @ApiOperation(value = "删除临时模板文件")
    public ResponseMsg<Boolean> deleteTempTemplate(@ApiParam(value = "模板文件名称", name = "templateName", required
            = true) @RequestBody TemplateDTO templateDTO) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.deleteTempTemplate(templateDTO));
        return responseMsg;
    }

    @GetMapping("/downloadTemplate")
    @ApiOperation(value = "下载模板")
    public void downloadTemplate(@ApiParam(value = "templateId", name = "templateId", required = true) @RequestParam String templateId,
                                        HttpServletResponse response) throws Exception {
        templateService.downloadTemplate(templateId, response);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "模板文件上传")
    public ResponseMsg<List<String>> upload(@ApiParam(value = "templateDTO", name = "templateDTO") TemplateDTO templateDTO) throws Exception{
        ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.upload(templateDTO));
        return responseMsg;
    }

    @GetMapping("/getTemplateListByName")
    @ApiOperation(value = "根据名称模糊查询模板列表")
    public ResponseMsg<List<TemplateDTO>> getTemplateListByName(@ApiParam(value = "模板名称", name = "templateName") @RequestParam(required = false) String templateName) {
        ResponseMsg<List<TemplateDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(templateService.getTemplateListByName(templateName));
        return responseMsg;
    }
}
